package com.zhj.bz.algorithm.sort.select;

import com.zhj.tool.CallBack;
import com.zhj.tool.TimeTools;

import java.util.Arrays;

/**
 * @author zhj
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] arr = {-5, 9, 2, 10, -1};
        sort(arr);
        System.out.println(Arrays.toString(arr));

        int[] bigArr = new int[80000];
        for (int i = 0; i < 80000; i++) {
            bigArr[i] = (int) (Math.random() * 80000);
        }
        System.out.println("选择排序：");
        TimeTools.useTime(new CallBack(){
            //定义execute方法
            public void execute(){
                sort(bigArr);
            }
        });
    }

    public static void sort(int[] arr) {
        int min = 0;
        int minIndex = 0;
        for (int i = 0; i < arr.length - 1; i++) {
            min = arr[i];
            minIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < min) {
                    min = arr[j];
                    minIndex = j;
                }
            }
            if (min != i) {
                arr[minIndex] = arr[i];
                arr[i] = min;
            }
        }
    }
}
